onlineMusic.controller('player', ['$rootScope', '$scope', '$http',
    function ($rootScope, $scope, $http) {
    
    $rootScope.playlist;
    $rootScope.current_song;
    $rootScope.next_song;

    $scope.voteToRemove = function(id, $event){
        $http.get('api/badvote/'+id).success(function(data) {
           // $($event.target).remove();
            $scope.updatePlaylist();
        });
    }
    
    $scope.voteLike = function(id, $event){
        $http.get('api/likevote/'+id).success(function(data) {
            $scope.updatePlaylist();
        });
    }
    
    $scope.updatePlaylist = function(){
        $http.get('api/playlist').success(function(data) {
            $rootScope.playlist = data;
            for(i = 0; i < data.length; i++){
                if(data[i].current_played == 1){
                    $rootScope.current_song = data[i];
                    $rootScope.next_song    = data[i+1];
                }
            }
        });  
    }
    
    $scope.nextTrack = function(){
        if($rootScope.current_song.change_track == 5){
            return true;
        }
        $http.get('api/changevote/'+$rootScope.current_song.song_id).success(function(data) {
            if(data == 'error'){
                alert('Хули ты жмакаешь, один раз тыкнул и сиди спокойно');
                return false;
            }
            
            $rootScope.current_song.change_track = $rootScope.current_song.change_track+1;
        });
    }
    
    $scope.updatePlaylist();
    
}]);
